package com.archermind;

import com.archermind.style.util.SessionUtil;
import com.feng.fengDUtil.BaseService;
import com.feng.fengDUtil.FengD;
import com.feng.fengDUtil.dao.SqlExecutor;
import com.feng.fengDUtil.dao.pojo.SqlLevelEnum;
import java.io.File;
import javax.servlet.MultipartConfigElement;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

@SpringBootApplication
public class StyleApplication  extends SpringBootServletInitializer {

	@Value("${spring.profiles.active}")
	private void init(String devOrProd) {
		SessionUtil.setDevOrProd(devOrProd);
	}

	public static void main(String[] args) {
		SpringApplication.run(StyleApplication.class, args);
	}

	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		//文件最大
		factory.setMaxFileSize("10240KB"); //KB,MB
		/// 设置总上传数据总大小
		factory.setMaxRequestSize("102400KB");
		String location = System.getProperty("user.dir") + "/data/tmp";
		File tmpFile = new File(location);
		if (!tmpFile.exists()) {
			tmpFile.mkdirs();
		}
		factory.setLocation(location);
		return factory.createMultipartConfig();
	}

	@Autowired
	public void setFengD(DataSource dataSource) {
		TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(dataSource);
		SqlExecutor.setShowLevel(
				(SessionUtil.isDev() ? SqlLevelEnum.SHOW_EXECUTE_SQL : SqlLevelEnum.SHOW_EXECUTE_SQL));
		BaseService.init(new FengD(proxy, true));
	}
}
